package btanalyzer.db.report;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

@Component("ReportPieceSize")
public class ReportPieceSize extends AbstractReport{

	@Override
	protected String reportOutptFileName() {
		return "ReportPieceSize.txt";
	}

	@Override
	protected void generateInt() {
		Query q = em.createNativeQuery("select m.PIECE_SIZE_IN_BYTES / 1024 " +
				"from metadata m order by m.PIECE_SIZE_IN_BYTES / 1024 asc");
		Query q1 = em.createNativeQuery("select max(m.PIECE_SIZE_IN_BYTES) / 1024 from metadata m");
		@SuppressWarnings("unchecked")
		List<BigInteger> res = q.getResultList();
		Map<Integer, Integer> results = new HashMap<Integer, Integer>();
		for (BigInteger i : res) {
			if (results.containsKey(i.intValue())) {
				results.put(i.intValue(), 1 + results.get(i.intValue()));
			} else {
				results.put(i.intValue(), 1);
			}
		}
		
		// print results
		
		pw.println("Wszystkich wyników:\t" + res.size());
		pw.println();
		pw.println("wielkosc fragmentu\tilosc torrentow");
		for (int i : results.keySet()) {
			pw.println(i + "\t" + results.get(i));
		}
	}

}
